package object3.package4;

import java.util.Arrays;
import java.util.Comparator;

public class Student1 implements Cloneable{
    private String name;
    private double score;

    public Student1(){
    }

    public Student1(String name, double score) {
        this.name = name;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public double getScore() {
        return score;
    }
    //重写相等判断
    @Override
    public boolean equals(Object o) {
        if(o == null){
            return false;
        }
        if(this == o){
            return true;
        }
        if(!(o instanceof Student1)){
            return false;
        }
        Student1 student1 = (Student1)o;
        return this.name.equals(student1.name) && this.score == student1.score;
    }

    //重写克隆
    @Override
    protected Student1 clone() throws CloneNotSupportedException {
        Student1 student1 = (Student1)super.clone();
        student1.name = new String(this.name);
        return student1;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", score=" + score +
                '}';
    }

    public static void main(String[] args) {
        Student1 s1 = new Student1("王双喜", 80.5);
        Student1 s2 = new Student1("王鑫宇", 93);
        Student1 s3 = new Student1("曾老七", 87.6);
        Student1[] students1 = new Student1[]{s1, s2, s3};
        System.out.println(Arrays.toString(students1));
        //Arrays.sort(students1, new Cmp());
        bubble(students1, new Cmp());
        System.out.println(Arrays.toString(students1));

        System.out.println(s1.equals(s2));
        Student1 s4 = s1;
        System.out.println(s1.equals(s4));
        try {
            s4 = s2.clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(s2);
        System.out.println(s4);
        System.out.println(s2 == s4);
        System.out.println(s2.getName() == s4.getName());
    }

    //升序冒泡排序
    public static void bubble(Student1[] students, Cmp cmp){
        //Cmp cmp = new Cmp();
        for(int i = 0; i < students.length - 1; i++){
            boolean flag = true;
            for(int j = 0; j < students.length - i - 1; j++){
                if(cmp.compare(students[j], students[j + 1]) > 0){
                    Student1 tmp = students[j];
                    students[j] = students[j + 1];
                    students[j + 1] = tmp;
                    flag = false;
                }
            }
            if (false){
                break;
            }
        }
    }
}